home *** CD-ROM | disk | FTP | other *** search
- Path: access1.digex.net!not-for-mail
- From: ell@access1.digex.net (Ell)
- Newsgroups: comp.lang.c++
- Subject: Re: Copy constructing an already default constructed object
- Date: 27 Jan 1996 04:41:20 GMT
- Organization: The Universe
- Message-ID: <4ecadg$5t5@news4.digex.net>
- References: <4e906b$stk@elaine32.Stanford.EDU> <4eal0n$hgq@dawn.mmm.com> <3108ef14.340699@nntp> <4ebehj$5i7@news.bridge.net>
- NNTP-Posting-Host: access1.digex.net
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- David Byrden (100101.2547@compuserve.com) wrote:
- :
- : >> A::operator =(const A& other)
- : >> {
- : >> b0 = other.b0 ;
- : >> b1 = other.b1;
- : >> }
- :
- : >> To me this seems like a major pain in the butt.
- :
- : That's precisely why the compiler will automatically generate a public
- : assignment operator with this behaviour, if you don't bother to write
- : one.
-
- Right, but the problem with a default op= is when the object to be copied
- has data members that are pointers you don't want copied to the copied to
- object. A default op= will copy the pointer address into the copied to
- object (deep copy), but in most cases all you want in the copied to object
- is the same values in the copied object (shallow copy). Defining your own
- op= allows you to copy the values of the copied object without copying its
- pointer addresses. See Meyers' Effective C++ book and the comp.lang.c++
- FAQ for more on this.
-
- : >> Do you have to overload = for all the contained operators too?
- :
- : >> All I really want to do is have the copy constructor invoked on the
- : >> piece that piece of memory. Why can't this be done?
-
- A copy ctor is generally for creating new objects from existing ones. op=
- is generally for copying an existing object's data values into another
- existing object. While the two ops often interact, they have separate
- primary puposes. Best to write both functions when your objects
- contain pointers because you'll generally get the wrong kind by default.
- See the above mentioned books on this point also.
-
- : Brien, I genuinely cannot understand what you are trying to say in these
- : two sentences. Sloppy English, like C++, can be a "major pain the the
- : butt".
-
- Sometimes people simply need to learn more C++, to phrase a question
- properly. The writer did pretty well by me for both questions.
-
- Elliott
-